package com.peration.connect;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class Sender {
//	private static String connectUrl = "http://10.0.2.2:8080/";
	private static String connectUrl = "http://diagosis-hrd.appspot.com/";
	public static String request(String url) {
		Log.i("Sending", url);
	//	BufferedReader in = null;
		try {
    		HttpClient Client = new DefaultHttpClient();
			HttpGet Request = new HttpGet(url);
			HttpResponse Response = Client.execute(Request);
			InputStream in = Response.getEntity().getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		//	String ReturnData = null;
			String ReturnData = reader.readLine();
			Log.i("Received",ReturnData);
			return ReturnData;
		} catch (Exception e) {
			e.printStackTrace();
			return "ERROR";
		} 
	}

	static public String urlEncode(String sUrl) {
		StringBuffer urlOK = new StringBuffer();
		for (int i = 0; i < sUrl.length(); i++) {
			char ch = sUrl.charAt(i);
			switch (ch) {
			case '<':
				urlOK.append("%3C");
				break;
			case '>':
				urlOK.append("%3E");
				break;
			case '/':
				urlOK.append("%2F");
				break;
			case ' ':
				urlOK.append("%20");
				break;
			case ':':
				urlOK.append("%3A");
				break;
			case '-':
				urlOK.append("%2D");
				break;
			case '+':
				urlOK.append("%2B");
				break;
			default:
				urlOK.append(ch);
				break;
			}
		}
		return urlOK.toString();
	}

	public static String fetchUrl(String data) {
		return request(connectUrl + urlEncode(data));
	}

}
